var util = require('../../../utils/util.js')
import Toast from '../../../dist/toast/toast';
import Dialog from '../../../dist/dialog/dialog';
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
      active: 0,
      isClassifyShow:false,
       popupShow:false,
      typeRadio:'1',
       mainClass:'新增主类时不用选',
      curNav: true,
      curIndex: 0,
      currentClassifyIndex:0,
      currentId: 0,
      parentId: 0,
      BackgroundMeImg:'http://deli-ai.test.upcdn.net/common/jinli.jpg',
      columns: [],
      mainIdList: [],
    // 列表高度
      scrollHeight: 0,
      classifyName:'',
      accountClassifyList:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    // 设置分类列表高度
    _this.setListHeight();
  },
  /**
   * 设置分类列表高度
   */
  setListHeight: function() {
    let _this = this;
    wx.getSystemInfo({
      success: function(res) {
        _this.setData({
          scrollHeight: res.windowHeight - 47,
        });
      }
    });
  },

  /**
   * 删除分类
   */
  delClassify(t){
    let classifyId = t.target.dataset.id;
    let type = t.target.dataset.type=='main'?'主':'子';
    Dialog.confirm({
      title: '删除确认',
      message: '给确定删除'+type+'分类'
    }).then(() => {
      var dataUrl = app.globalData.apiBase +
          "/blog/removeAccountClassify";
      util.ajax(dataUrl, "get", {
        id:classifyId
      }, (result,respCode)=>{
        if (respCode === '3000') {
        }else if(respCode === '0000'){
          this.getAccountClassifyList()
        }
      })
    })
  },
  /**
   * 一级分类：选中分类
   */
  selectNav: function(t) {
    let curNav = t.target.dataset.id,
    currentId = t.target.dataset.id,
      curIndex = parseInt(t.target.dataset.index);
    this.setData({
      curNav,
      curIndex,
      currentId:t.target.dataset.id,
      scrollTop: 0
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onRemindConfirm(event){
    const { picker, value, index } = event.detail;
    this.setData({
      popupShow: false,
      mainClass:`${value}`,
      currentClassifyIndex:`${index}`,
      parentId:this.data.mainIdList[`${index}`]
    });
  },
  getAccountClassifyList(){
    var dataUrl = app.globalData.apiBase +
      "/blog/getAccountClassifyList";
    util.ajax(dataUrl, "post", {
      // id:this.data.currentId==0?'':this.data.currentId
    }, (respResult)=>{
      this.setData({
        accountClassifyList:respResult==null?[]:respResult,
        curNav: (respResult==null?0:respResult.length) > 0 ? respResult[0].id : true,
      })
      let mainSelect=[]
      let mainIdList=[]
      this.data.accountClassifyList.forEach((item)=>{
        if(item.parentId==0){
          mainSelect.push(item.classifyName)
          mainIdList.push(item.id)
        }
      });
      this.setData({
        curNav:mainIdList[0],
        curIndex:0,
        columns:mainSelect,
        mainIdList:mainIdList
      })
    })
  },
  onStrategyCancel(){
    this.setData({
      popupShow: false
    })
  },
  showPopup(){
    this.setData({ popupShow: true });
  },
  onPopupClose() {
    this.setData({ popupShow: false });
  },
    onChange(event) {
    this.setData({
      currentIndex:`${event.detail + 1}`
    })
        wx.showToast({
            title: `切换到标签 ${event.detail + 1}`,
            icon: 'none'
        });
    },
    navigateBackClassify(){
    this.setData({
      isClassifyShow:false,
      classifyName:'',
      typeRadio:'1'
    })
  },
  classifyNameChange(event){
    this.setData({
      classifyName: event.detail
    });
  },
  onClassifyAddChange(event) {
    this.setData({
      typeRadio: event.detail
    });
    console.log(this.data.classifyName+"--"+this.data.typeRadio)
  },
  //新增账单分类提交
  nowFire(){
    if(this.data.classifyName==''){
      Toast('分类名称拔忘记写')
      return;
    }
    var dataUrl = app.globalData.apiBase +
        "/blog/addOrUpdateClassify";
    util.ajax(dataUrl, "post", {
      parentId:this.data.parentId,
      classifyName: this.data.classifyName,
      accountType:this.data.typeRadio,
    }, (respResult,code)=>{
      if(code==='0000'){
         this.setData({
          curNav:this.data.parentId,
          curIndex:this.data.currentClassifyIndex
        });
        this.getAccountClassifyList()
        this.navigateBackClassify()
      }else{
        wx.showToast({
          title: '新增失败！',
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
    /**
     * 新增分类
     */
    addClassify(){
       this.setData({
           isClassifyShow:true
       })
    },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAccountClassifyList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})
